package org.jeecg.common.aspect;

import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.aspect.annotation.ThirdPartyEntity;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 第三方实体工具类
 *
 * @author zhanglei01
 * @date 2022/03/08
 */
public class ThirdPartyEntityUtils {

    /**
     * 验证忽略
     * 实体类添加 @ThirdPartyEntity(ignore = true) 可忽略返回
     *
     * @param tableInfo 表信息
     * @return {@link String}
     */
    public static String validateIgnore(TableInfo tableInfo) {
        String allSqlSelect = tableInfo.getAllSqlSelect();
        String[] split = allSqlSelect.split(",");
        if (split.length > 0) {
            List<String> validateValue = Arrays.stream(split).collect(Collectors.toList());
            List<TableFieldInfo> fieldList = tableInfo.getFieldList();
            for (TableFieldInfo tableFieldInfo : fieldList) {
                //判断是否添加忽略返回注解
                ThirdPartyEntity annotation = tableFieldInfo.getField().getAnnotation(ThirdPartyEntity.class);
                if (annotation != null && annotation.ignore()) {
                    validateValue.remove(tableFieldInfo.getColumn());
                }
            }
            if (validateValue.isEmpty()) {
                return "";
            }
            allSqlSelect = StringUtils.join(validateValue, ",");
        }
        return allSqlSelect;
    }


}
